// GLOBAL
var nbr_results = {}
nbr_results['YT'] = 10;

var playlist = new Array();
var current_song = -1;

// Youtube player
var player;
function onYouTubePlayerAPIReady() {
	player = new YT.Player('player', {
		height : '250',
		width : '350',
		playerVars : {
			'autoplay' : 1,
			'frameborder' : 0
		},
		events : {
			'onStateChange' : onPlayerStateChange,
		}
	});
}

function onPlayerStateChange(event) {
	if (event.data == YT.PlayerState.ENDED) {
		nextSong();
	}
}

// Action
function show_result(website, data) {
	if (website == 'YT') {
		for (i = 0; i < data.length; i++) {
			html_result = '<tr><td><a href="#" class="play_button">Play</a></td>';
			html_result += '<td><a href="#" class="add_to_playlist_button">Add to playlist</a></td>';
			html_result += '<td class="title">' + data[i].title + '</td>';
			html_result += '<td class="id" hidden>' + data[i].id + '</td></tr>';

			$('#result .loading').hide();
			$('#search_button').removeAttr("disabled");
			$("#result .YT_table").append(html_result)
		}

		$('.YT .play_button').click(
				function() {
					event.preventDefault();

					var song = {};
					song.website = 'YT';
					song.id = $(this).parent('td').siblings('td[class="id"]')
							.text();
					song.title = $(this).parent('td').siblings(
							'td[class="title"]').text();
					song.position = playlist.length;

					addSongToPlaylist(song);

					play(song);
				});

		$('.YT .add_to_playlist_button').click(
				function() {
					event.preventDefault();

					var song = {};
					song.website = 'YT';
					song.id = $(this).parent('td').siblings('td[class="id"]')
							.text();
					song.title = $(this).parent('td').siblings(
							'td[class="title"]').text();
					song.position = playlist.length;

					addSongToPlaylist(song);
				});
	}
}

function play(song) {
	old_song = current_song;
	current_song = song.position;

	// Update current song number
	$('.playlist_header .playlist_current_position').text(current_song + 1);

	$('.preview .song_title').html(song.title);
	if (song.website == 'YT') {
		player.loadVideoById(song.id);
	}

	if (!$('#playlist').is(":visible")) {
		$('#playlist').show();// "slide", { direction:
		// "right" }, 1000);
	}

	// We update the now is playing
	if (old_song >= 0) {
		select_old = '.playlist_table .' + (old_song + 1) + ' .td_remove';
		$(select_old).html('<a href="#" class="remove_song">Remove</a>');
		select_old += ' .remove_song';
		$(select_old).click(
				function() {
					event.preventDefault();
					var position = $(this).parent('td').siblings(
							'.song_position').text() - 1;
					removeSongToPlaylist(position);
				});
	}

	select_new = '.playlist_table .' + (current_song + 1) + ' .td_remove';
	$(select_new).html('Now playing');

}

function nextSong() {
	if (current_song < playlist.length - 1) {
		play(playlist[current_song + 1]);
	}
}

function prevSong() {
	if (current_song > 0) {
		play(playlist[current_song - 1]);
	}
}

function addSongToPlaylist(song) {
	playlist.push(song);

	// Update number of song
	$('.playlist_header .playlist_count').text(playlist.length);

	// Add row
	html = '<tr class="' + (song.position + 1) + '"><td class="song_position">'
			+ (song.position + 1) + '</td>';
	html += '<td class="td_title">'
			+ song.title
			+ '</td><td class="td_remove"><a href="#" class="remove_song">Remove</a></td></tr>';
	$('.playlist_table').append(html);

	select = '.playlist_table tr[class="' + (song.position + 1)
			+ '"] .remove_song'
	$(select).click(
			function() {
				event.preventDefault();
				var position = $(this).parent('td').siblings('.song_position')
						.text() - 1;
				removeSongToPlaylist(position);
			});

	if (!$('#playlist').is(":visible")) {
		$('#playlist').show();// "slide", { direction:
		// "right" }, 1000);
	}
	
	$(".playlist_table").tableDnD();

}

function removeSongToPlaylist(position) {
	// update cur position
	if (current_song > position) {
		current_song = current_song - 1;
	}

	// remove from the array
	playlist.splice(position, 1);

	// clean the array
	for (i = 0; i < playlist.length; i++) {
		playlist[i].position = i;
	}

	// repaint the list
	repaintPlaylist();
}

function updatePlaylist() {
	var temp = new Array();
	$('.playlist_table tr').each(function(index) {
	});
}

function repaintPlaylist() {
	$('.playlist_list .playlist_table').empty();

	for (i = 0; i < playlist.length; i++) {
		song = playlist[i];

		// Add row
		html = '<tr class="' + (song.position + 1)
				+ '"><td class="song_position">' + (song.position + 1)
				+ '</td>';
		html += '<td class="td_title">'
				+ song.title
				+ '</td><td class="td_remove"><a href="#" class="remove_song">Remove</a></td></tr>';
		$('.playlist_table').append(html);
	}

	// Update number of song
	$('.playlist_header .playlist_count').text(playlist.length);

	// Update current song number
	$('.playlist_header .playlist_current_position').text(current_song + 1);

	// Now playing
	select = '.playlist_table .' + (current_song + 1) + ' .td_remove';
	$(select).html('Now playing');

	$('.playlist_table .remove_song').click(function() {
		event.preventDefault();
		position = $(this).parent('td').siblings('.song_position').text() - 1;
		removeSongToPlaylist(position);
	});
	
	$(".playlist_table").tableDnD();

}

function search() {
	var string = $('#search_box').val();
	$.post('/search/ajax', {
		w : 'YT',
		s : string,
		n : nbr_results['YT']
	}, function(data) {
		data = jQuery.parseJSON(data);
		show_result('YT', data);
	});
}

$(document).ready(function() {
	// Init page
	$('#playlist').hide();
	$('#result .loading').hide();
	
	//Search Bar
	$('#search_button').unbind("click").click(function() {
		$('#search_button').attr("disabled", "disabled");
		$('#result .loading').show();
		event.preventDefault();
		$('#result .YT_table').empty();
		search();
	})

	$(search_box).keypress(function(e) {
		if (e.which == 13) {
			$('#search_button').click();
		}
	});

	//More result button
	$('.more_results_YT').unbind("click").click(function() {
		event.preventDefault();
		nbr_results['YT'] += 10;
		search();
	})

	//PLaylist button (Previous and Next)
	$('.playlist_prev_button').unbind("click").click(function() {
		event.preventDefault();
		prevSong();
	})

	$('.playlist_next_button').unbind("click").click(function() {
		event.preventDefault();
		nextSong();
	})
	
	// Initialise the table
    $(".playlist_table").tableDnD();

});